package com.neuedu.spring.tx.service;

import com.neuedu.spring.tx.dao.AccountDao;
import com.neuedu.spring.tx.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目：spring-java4
 * 创建时间：  2022-03-22   9:09
 * 作者 :jshand
 * 描述 :
 */
@Service
public class AccountService {
    @Autowired
    AccountDao accountDao;

    /**
     * 转账的操作
     */
    @Transactional(propagation= Propagation.REQUIRED ,isolation = Isolation.DEFAULT)
    public void  transfer(){
        //从张三的账户-10
        Account account = new Account();
        account.setId(1);
        account.setName("张三");
        account.setAmount(100-10d);
        int update = accountDao.update(account);

        System.out.println("update = " + update);
//        int fate = 0/0; //模拟一系列运算,可能出错

        //给李四的账号 +10
        Account account2 = new Account();
        account2.setId(2);
        account2.setName("李四");
        account2.setAmount(100+10d);
        update = accountDao.update(account2);

        System.out.println("update = " + update);

    }





}
